package com.hb.znpt.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.hb.znpt.BR;
import com.hb.znpt.R;
import com.hb.znpt.adapter.GridImageAdapter;
import com.hb.znpt.databinding.ActivityUploadBinding;
import com.hb.znpt.image.GlideEngine;
import com.hb.znpt.utils.FullyGridLayoutManager;
import com.hb.znpt.viewmodel.UploadViewModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnItemClickListener;
import com.luck.picture.lib.tools.ScreenUtils;
import com.unitlib.base.base.BaseActivity;

import java.util.List;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/9/9 16:59
 */
public class UploadActivity extends BaseActivity<ActivityUploadBinding, UploadViewModel> {

    private GridImageAdapter adapter;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_upload;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initBackTitle(binding.toolbar);
        initRecyclerView();
    }

    @Override
    public void initViewObservable() {

    }

    /**
     * Init recycler view.
     * 初始化RecyclerView
     */
    public void initRecyclerView() {
        adapter = new GridImageAdapter(context, new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                viewModel.photo(adapter);
            }
        });
        FullyGridLayoutManager photoFM = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        binding.recyclerView.setLayoutManager(photoFM);
        binding.recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(this, 8), false));
        binding.recyclerView.setAdapter(adapter);
        adapter.setSelectMax(1);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                List<LocalMedia> selectList = adapter.getData();
                PictureSelector.create(UploadActivity.this)
                        .themeStyle(R.style.picture_default_style) // xml设置主题
                        .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)// 设置相册Activity方向，不设置默认使用系统
                        .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                        .imageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                        .openExternalPreview(position, selectList);
            }
        });
    }
}
